// eslint-disable-next-line import/no-extraneous-dependencies
import Mock from 'mockjs';
import fs from 'fs';

export default {
  'GET /getMockList': (req, res) => {
    const { pageSize = 10 } = req.query;
    const data = Mock.mock({
      success: true,
      data: {
        [`records|${[pageSize]}`]: [
          {
            name: '@name',
            age: '@integer(10,100)',
            address: '@county(true)',
            'tags|2': ['cool', 'teacher', 'loser'],
            key: '@id',
          },
        ],
        total: 400,
        req: req.data,
        query: req.query,
      },
    });
    setTimeout(() => {
      res.json(data);
    }, Math.random() * 2000);
  },
  'POST /saveSnapshot': (req, res) => {
    const { snapshot } = req.body;
    const ws = fs.createWriteStream(`./public/images/${snapshot}.jpeg`);
    ws.write(req.files[0].buffer, err => {
      if (!err) {
        res.send({ success: true, path: `/images/${snapshot}.jpeg` });
      } else {
        res.send({ success: false });
      }
      return true;
    });
  },
};
